//
//  SFRetweetStatusView.swift
//  SFWeiBo
//
//  Created by 孙凡 on 16/4/12.
//  Copyright © 2016年 Edward. All rights reserved.
//

import UIKit
import SnapKit

class SFRetweetStatusView: UIView {

    var myConstraint: Constraint?

    var statusViewModel: SFStatusViewModel?{

        didSet{

            //给控件设置数据
            textLabel.text = statusViewModel?.retweelString

            //删除上一个自己的约束
            self.myConstraint?.uninstall()

            //取出pic_urls
            let pic_urls = statusViewModel?.status?.retweeted_status?.pic_urls

            //判断有没有图片,顺便传值
            if pic_urls != nil && pic_urls!.count > 0 {
                //有值

                //传值
                pictureView.pic_urls = pic_urls

                //取消隐藏
                pictureView.hidden = false
                //修改约束
                self.snp_makeConstraints(closure: { (make) in

                    self.myConstraint = make.bottom.equalTo(pictureView).constraint
                })

            } else{

                //没值

                //隐藏
                pictureView.hidden = true
                //修改约束
                self.snp_makeConstraints(closure: { (make) in
                    self.myConstraint = make.bottom.equalTo(textLabel).constraint
                })
            }
            
        }
        
    }

    override init(frame: CGRect) {
        super.init(frame: frame)

        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    private func setupUI(){

        self.backgroundColor = UIColor(white: 239/255, alpha: 1)

        addSubview(textLabel)

        //添加图片view
        addSubview(pictureView)

        //设置布局
        textLabel.snp_makeConstraints { (make) in

            make.leading.trailing.top.equalTo(self).offset(SFStatusCellMargin)
        }

        pictureView.snp_makeConstraints { (make) in

            make.top.equalTo(textLabel.snp_bottom)
            make.leading.equalTo(textLabel)
        }

        self.snp_makeConstraints { (make) in

            myConstraint = make.bottom.equalTo(textLabel.snp_bottom).offset(SFStatusCellMargin).constraint
        }

    }


    //懒加载控件
    lazy var textLabel: UILabel = {

        let label = UILabel()

        label.numberOfLines = 0

        label.text = "今天突然脑子抽筋,看着自己手,突然醒悟它什么时候长的那么大了,上一次这么细看它的时候好像是十六七岁,远比现在稚嫩,心里忽然就变得很恐慌,不晓得这都是什么时候发生的事.然后在想想,变得快的起止是这一件事,好多人离开,好多人进入,新的却无法代替旧的.旧的人还在,却也早就不是她"

        return label

    }()

    lazy var pictureView: SFStatusPictureView = {

        let p = SFStatusPictureView(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())

        return p
    }()


}
